﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace mvchotleweb.Controllers
{
    [Authorize]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        hotel.BLL.tb_user userbll = new hotel.BLL.tb_user();
        hotel.BLL.tb_webinfo webbll = new hotel.BLL.tb_webinfo();

        [Authorize]
        public ActionResult Index()
        {
            hotel.Model.tb_webinfo webmod = new hotel.Model.tb_webinfo();
            webmod = webbll.GetModel(1);
            ViewBag.Message = webmod.web_name;
            if (Session["USER"] == null)
                return RedirectToAction("Index", "User");
            switch ((Session["USER"] as hotel.Model.tb_user).type)
            {
                case 1: ViewData["type"] = "超级管理员"; break;
                case 2: ViewData["type"] = "网站管理"; break;
                case 3: ViewData["type"] = "入住管理"; break;
                default:
                    ViewData["type"] = "错误"; break;
            }
            ViewData["user"] = (Session["USER"] as hotel.Model.tb_user).username.ToString();
            return View();
        }

        #region  ManageTree 后台树的管理
        //  [HttpPost]
        public ActionResult UserManageTree()
        {
            // hotel.Model.tb_user usermod = Session["USER"] as hotel.Model.tb_user;
            string json = @"[{""id"":1,""text"":""网站信息管理"",""children"":[]},{""text"":""用户管理"",""state"":""closed"", 
                            ""iconCls"":""icon-user_manage"",""children"":[{""text"":""用户管理"",""iconCls"":""icon-user_manage"",
                           ""attributes"":{""url"":""/admin/UserMange""}}]}]";
            string json1 = @"[{""id"": 1,""text"": ""用户管理"",""children"": [{""text"": ""修改密码"",""attributes"": {""url"": ""/admin/ChangePwd""}}]}]";
            //  if (usermod.type!=1)
            //     return Content(json1);
            return Content(json);
        }

        public ActionResult WebManageTree()
        {
            string json = @"[{
        ""id"": 1,
        ""text"": ""前台页面管理"",
        ""children"": [
            {
                ""text"": ""图片轮播管理"",
                ""attributes"": {
                    ""url"": ""/admin/FlashAdManage""
                }
            },
            
			{
                ""text"": ""网站提示信息管理"",
                ""attributes"": {
                    ""url"": ""/admin/ManageInfo""
                }
            },
            {
                ""text"": ""留言信息管理"",
                ""attributes"": {
                    ""url"": ""/admin/ManageMessage""
                }
            }
        ]
    }
]";

            /*{
                ""text"": ""新闻类型管理"",
                ""attributes"": {
                    ""url"": ""/admin/NewsTypeManage""
                }
            },
            {
                ""text"": ""新闻管理"",
                ""attributes"": {
                    ""url"": ""/admin/NewsManage""
                }
            },*/
            return Content(json);
        }

        public ActionResult roomManageTree() 
        {
            string json = @"[{
        ""id"": 1,
        ""text"": ""客房管理"",
        ""children"": [
            {
                ""text"": ""添加房间类型"",
                ""attributes"": {
                    ""url"": ""/admin/addroomtype""
                }
            },
			{
                ""text"": ""管理房间信息"",
                ""attributes"": {
                    ""url"": ""/admin/roomInfoManage""
                }
            }
        ]
        }
        ]";
            return Content(json);
        }

        public ActionResult BookingManageTree()
        {
            string json = @"[{
        ""id"": 1,
        ""text"": ""客房预定管理"",
        ""children"": [
            {
                ""text"": ""预定信息管理"",
                ""attributes"": {
                    ""url"": ""/admin/bookingmanage""
                }
            },
			{
                ""text"": ""退房结算"",
                ""attributes"": {
                    ""url"": ""/admin/Settlement""
                }
            }
        ]
        }]";
            return Content(json);
        }

        #endregion  ManageTree 后台树的管理

        public ActionResult bookingmanage()
        {
            return View();
        }
        public ActionResult Settlement()
        {
            return View();
        }







        public ActionResult ManageMessage()
        {
            return View();
        }
        public ActionResult Getmessagelist()
        {
            hotel.BLL.tb_message blme = new hotel.BLL.tb_message();
            string res = JsonConvert.SerializeObject(blme.GetListByPage("", "", 0, 10).Tables[0]);
            return Content(res);
        }
        public ActionResult delmessage()
        {
            int id = Convert.ToInt32(Request.Form["ID"]);
            hotel.BLL.tb_message blme = new hotel.BLL.tb_message();
            bool re = blme.Delete(id);
            return Content(re.ToString());
        }
        public ActionResult Respus()
        {
            int id = Convert.ToInt32(Request.Form["rid"]);
            string re = Request.Form["rcontext"];
            hotel.BLL.tb_message blme = new hotel.BLL.tb_message();
            hotel.Model.tb_message memod = blme.GetModel(id);
            memod.respuse = re;
            bool res = blme.Update(memod);
            return Content(res.ToString());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult Getbookinglist()
        {
            hotel.BLL.view_booking blme = new hotel.BLL.view_booking();
            
             string res = JsonConvert.SerializeObject(blme.GetListByPage("", "", 0, 10).Tables[0]);
            return Content(res);
        }


        public ActionResult Getsettlist()
        {
            hotel.BLL.view_booking blme = new hotel.BLL.view_booking();

            string res = JsonConvert.SerializeObject(blme.GetListByPage("chekin = 1", "", 0, 10).Tables[0]);
            return Content(res);
        }

        public ActionResult Optbooking()
        {
            int id = Convert.ToInt32(Request.Form["ID"]);
            int op = Convert.ToInt32(Request.Form["OP"]);
            bool res = false;
            hotel.BLL.tb_booking bllbook = new hotel.BLL.tb_booking();
            hotel.Model.tb_booking viewbook = bllbook.GetModel(id);
            
            switch (op)
            {
                case 1:
                    viewbook.chekin = true;
                    res = bllbook.Update(viewbook);
                    break;
                case 2:
                    viewbook.is_chack = true;
                    res = bllbook.Update(viewbook);
                    break;
                case 3:
                    res = bllbook.Delete(id);
                    break;
            }
   
            return Content(res.ToString());
        }

        public ActionResult sttel()
        {
            int id = Convert.ToInt32(Request.Form["ids"]);
            hotel.BLL.tb_booking bokbll = new hotel.BLL.tb_booking();
            hotel.BLL.tb_roomtype rombll =new hotel.BLL.tb_roomtype();
            hotel.Model.tb_booking  mod = bokbll.GetModel(id);
            DateTime dt = DateTime.Now;
            TimeSpan time = dt.Subtract(mod.time);
            double Total = (double)(rombll.GetModel(mod.room).Price);
            double sum = Total * time.TotalDays;
            string str = "<Script>alert('应付金额：" + sum + "<br/> 用户姓名：" + mod.name + "<br /> ');javascript:history.go(-1);</script>";
            return Content("s");
        }
        #region  管理界面调用

        #region  图片轮播管理

        public ActionResult FlashAdManage()
        {
            return View();
        }

        public ActionResult Readpiclist() //读取Flash轮播表
        {
            hotel.BLL.tb_flasad fdll = new hotel.BLL.tb_flasad();
            string res = JsonConvert.SerializeObject(fdll.GetListByPage("", "", 0, 10).Tables[0]);
            return Content(res);
        }

        public ActionResult UpDataImgInfo()
        {
            return View();
        }
       
        public ActionResult AddImg()
        {
            string filename = "123";
            
            hotel.Model.tb_flasad fld = new hotel.Model.tb_flasad();
            hotel.BLL.tb_flasad flll = new hotel.BLL.tb_flasad();
            string txtalt = Request.Form["txtalt"];
            bool is_enable = Request.Form["checkbox"] == "true,false"?true:false;
            foreach (string sfile in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[sfile];
                if (file == null)
                    continue;
                string miniType = file.ContentType;
                Stream fileStream = file.InputStream;
                string path = Server.MapPath("~/Images");
                filename = Path.GetFileName(file.FileName);
                file.SaveAs(Path.Combine(path, filename));
            }
            if (filename == "123")
                return Content("<Script>alert('上传失败');javascript:history.go(-1);</script>");
            
            fld.alt = txtalt;
            fld.imgurl = "Images/" + filename;
            fld.is_enable = is_enable;

            flll.Add(fld);
            return Content("<Script>alert('上传成功');javascript:history.go(-1);</script>");
        }

        public ActionResult DelImg()
        {
            hotel.BLL.tb_flasad fdll = new hotel.BLL.tb_flasad();
            bool res;
            int id = Convert.ToInt32(Request.Form["ID"]);
            res = fdll.Delete(id);
            return Content(res.ToString());
        }


        #endregion  

        #region  房间管理
        public ActionResult addroomtype()
        {
            return View();
        }

        public ActionResult roomInfoManage()
        {
            return View();
        }


        public ActionResult addRoom()
        {
            string filename = "123";
            string path;
            hotel.BLL.tb_room rombll = new hotel.BLL.tb_room();
            hotel.BLL.tb_Image imgbll = new hotel.BLL.tb_Image();
            hotel.Model.tb_room modrom = new hotel.Model.tb_room();
            hotel.Model.tb_Image imgmod = new hotel.Model.tb_Image();
            modrom.title = Request.Form["roomName"];
            modrom.typeID = Convert.ToInt32(Request.Form["Selecttype"]);
            modrom.context = Request.Form["info"];

            foreach (string sfile in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[sfile];
                if (file == null)
                    continue;
                string miniType = file.ContentType;
                Stream fileStream = file.InputStream;
                path = Server.MapPath("~/Images");
                filename = Path.GetFileName(file.FileName);
                file.SaveAs(Path.Combine(path, filename));
            }
            if (filename == "123")
                return Content("<Script>alert('添加失败');javascript:history.go(-1);</script>");
            imgmod.url = "/Images/" + filename;
            ;
            modrom.ImageID = imgbll.Add(imgmod);
            rombll.Add(modrom);
            return Content("<Script>alert('添加传成功');javascript:history.go(-1);</script>");
        }

        public ActionResult DelRoom()
        {
            bool res;
            int id = Convert.ToInt32(Request.Form["ID"]);
            hotel.BLL.tb_room rl = new hotel.BLL.tb_room();
            res = rl.Delete(id);
            return Content(res.ToString());
        }

        public ActionResult GetRoomList()
        {
            hotel.BLL.View_room fdll = new hotel.BLL.View_room();
            string res = JsonConvert.SerializeObject(fdll.GetModelList(""));
            return Content(res);
        }
        #endregion

        #region  前台提示信息管理

        [Authorize]
        public ActionResult ManageInfo()
        {
            return View();
        }

        public ActionResult GetMenageType_json()
        {
            hotel.BLL.tb_info idll = new hotel.BLL.tb_info();

            string res = JsonConvert.SerializeObject(idll.GetListByPage("", "", 0, 100).Tables[0]);
            return Content(res);
        }

        public ActionResult GetInfomation()
        {
            hotel.BLL.tb_info idll = new hotel.BLL.tb_info();
            string re;
            int id = Convert.ToInt32( Request.Form["id"]);
            hotel.Model.tb_info info = idll.GetModel(id);
            if (info != null)
                re = info.text;
            else
                re = "0";
            return Content(re);
        }

        public ActionResult UpdataInfomation()
        {
            hotel.BLL.tb_info idll = new hotel.BLL.tb_info();
            string re;
            int id = Convert.ToInt32(Request.Form["id"]);
            string text = Request.Form["text"];
            hotel.Model.tb_info info = idll.GetModel(id);
            info.text = text;
            idll.Update(info);
            if (idll.Update(info))
                re = "1";
            else
                re = "0";
            return Content(re);
        }





        #endregion

      /*  #region  新闻管理


        public ActionResult NewsTypeManage()
        {
            return View();
        }

        public ActionResult GetNwsTypeList()
        {
            hotel.BLL.tb_news_type newdll = new hotel.BLL.tb_news_type();
            string res = JsonConvert.SerializeObject(newdll.GetAllList().Tables[0]);
            return Content(res);
        }



        public ActionResult DeletNewsType()
        {
            int id = Convert.ToInt32(Request.Form["ID"]);
            hotel.BLL.tb_news_type bllnews = new hotel.BLL.tb_news_type();
            return Content(bllnews.Delete(id).ToString());
        }

        public ActionResult GetNewsTypeTitle()
        {
            int id = Convert.ToInt32(Request.Form["ID"]);
            hotel.BLL.tb_news_type bllnews = new hotel.BLL.tb_news_type();
            string st = bllnews.GetModel(id).title;
            return Content(st);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddNewsType()
        {
            string title = Request.Form["title"];
            hotel.BLL.tb_news_type bllnews = new hotel.BLL.tb_news_type();
            hotel.Model.tb_news_type modnews = new hotel.Model.tb_news_type();
            modnews.title = title;
            modnews.time = DateTime.Now;
            return Content(bllnews.Add(modnews).ToString());
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdataNewsType()
        {
            string title = Request.Form["title"];
            int id = Convert.ToInt32(Request.Form["ID"]);
            hotel.BLL.tb_news_type bllnews = new hotel.BLL.tb_news_type();
            hotel.Model.tb_news_type modnews = new hotel.Model.tb_news_type();
            modnews = bllnews.GetModel(id);
            modnews.title = title;
            modnews.time = DateTime.Now;
            bllnews.Update(modnews);
            return Content("ok");
        }







        public ActionResult NewsManage()
        {
            return View();
        }

        public ActionResult GetNewsList()
        {
            hotel.BLL.view_newlist newdll = new hotel.BLL.view_newlist();
            string res = JsonConvert.SerializeObject(newdll.GetAllList().Tables[0]);
            return Content(res);
        }


        public ActionResult GetNewsTypeList()
        {
            hotel.BLL.tb_news_type newdll = new hotel.BLL.tb_news_type();
            string res = JsonConvert.SerializeObject(newdll.GetAllList().Tables[0]);
            return Content(res);
        }

        public ActionResult GetNewsContext()
        {
            int id = Convert.ToInt32(Request.Form["ID"]);
            hotel.BLL.tb_news bllnews = new hotel.BLL.tb_news();
            hotel.Model.tb_news modnews = bllnews.GetModel(id);;
            return Content(modnews.content);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult addNews()
        {
            string title = Request.Form["title"];
            string html = Request.Form["text"];
            int type = Convert.ToInt32(Request.Form["type"]);
            hotel.BLL.tb_news bllnews = new hotel.BLL.tb_news();
            hotel.Model.tb_news modnews = new hotel.Model.tb_news();
            modnews.title = title;
            modnews.type = type;
            modnews.poster = (Session["USER"] as hotel.Model.tb_user).ID;
            modnews.time = DateTime.Now;
            modnews.content = html;
            return Content(bllnews.Add(modnews).ToString());
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdataNews()
        {
            int id = Convert.ToInt32(Request.Form["ID"]);
            string html = Request.Form["text"];
            hotel.BLL.tb_news bllnews = new hotel.BLL.tb_news();
            hotel.Model.tb_news modnews = bllnews.GetModel(id);
            modnews.poster = (Session["USER"] as hotel.Model.tb_user).ID;
            modnews.time = DateTime.Now;
            modnews.content = html;
            return Content(bllnews.Update(modnews).ToString());
        }

        public ActionResult DeletNews()
        {
            int id = Convert.ToInt32(Request.Form["ID"]);
            hotel.BLL.tb_news bllnews = new hotel.BLL.tb_news();
            return Content(bllnews.Delete(id).ToString());
        }


        #endregion
        */

        #region  用户管理
        [Authorize]


        [Authorize]
        public ActionResult UserMange()
        {
            if ((Session["USER"] as hotel.Model.tb_user).type > 1)
                return Content("<Script>alert('对不起您无权操作');javascript:history.go(-1);</script>");
            return View();
        }

        [Authorize]
        public ActionResult ReadUser()
        {
            string res = JsonConvert.SerializeObject(userbll.GetListByPage("", "", 0, 10).Tables[0]);
            return Content(res);
        }

        public ActionResult test()
        {
            return Content(JsonConvert.SerializeObject(Session["USER"]as hotel.Model.tb_user));
        }

        [Authorize]
        public ActionResult AddUser()
        {
            string username = Request.Form["name"];
            string pwd = Request.Form["pwd"];
            string type = Request.Form["type"];
            hotel.Model.tb_user tmpuser = new hotel.Model.tb_user();
            tmpuser.username = username;
            tmpuser.passworld = pwd;
            tmpuser.type =int.Parse(type);
            tmpuser.is_eable = true;
            int res;
            if (userbll.GetModel(username) == null) //判断当前有无该用户
                res = userbll.Add(tmpuser);
            else {
                res = -1;
            }
            return Content(res.ToString());
        }

        [Authorize]
        public ActionResult updatauser()
        {
            bool res;
            string username = Request.Form["name"];
            string pwd = Request.Form["pwd"];
            string type = Request.Form["type"];
            int id = Convert.ToInt32( Request.Form["ID"]);
            hotel.Model.tb_user tmpuser = new hotel.Model.tb_user();
            tmpuser.username = username;
            tmpuser.passworld = pwd;
            tmpuser.type = int.Parse(type);
            tmpuser.is_eable = true;
           
            res = userbll.Updatebyname(tmpuser);
            return Content(res.ToString());
        }

        public ActionResult Deluser()
        {
            bool res;
            int id =Convert.ToInt32( Request.Form["ID"]);
            res = userbll.Delete(id);
            return Content(res.ToString());
        }
        #endregion

        #endregion  管理界面调用
    }
}
